#!/bin/sh
#
# copy over NetworkManager configuration files and set proper
# permissions.
#

set -e

. /usr/sbin/balena-config-vars

if [ ! -f "$CONFIG_PATH" ]; then
	echo "balena-net-config: $CONFIG_PATH does not exist."
	exit 1
else
	echo "balena-net-config: Found config.json in $CONFIG_PATH ."
fi

if [ ! -d "$BALENA_BOOT_MOUNTPOINT" ]; then
	echo "balena-net-config: $BALENA_BOOT_MOUNTPOINT doesn't exist."
	exit 1
fi

CONFIG_NM=${BALENA_BOOT_MOUNTPOINT}/system-connections/
if [ -d "$CONFIG_NM" ]; then
	cp -r "$CONFIG_NM" /etc/NetworkManager/
	chmod 600 /etc/NetworkManager/system-connections/*
	sync -f "/etc/NetworkManager/"
fi

DISPATCHER_NM=${BALENA_BOOT_MOUNTPOINT}/dispatcher.d/
if [ -d "$DISPATCHER_NM" ]; then
	cp -r "$DISPATCHER_NM" /etc/NetworkManager/
	chmod 755 /etc/NetworkManager/dispatcher.d/*
	sync -f "/etc/NetworkManager/"
fi

# set the wifi regulatory domain; keep the devices default if unset
if [ ! -z "$COUNTRY" ]; then
	iw reg set "$COUNTRY"
fi

# add additional DNS servers to dnsmasq configuration
DNS_SERVER_FILE=/run/dnsmasq.servers
echo > $DNS_SERVER_FILE
for dns in $DNS_SERVERS; do
	echo "server=$dns" >> $DNS_SERVER_FILE
	sync -f "$DNS_SERVER_FILE"
done

# Setup DNS bridge interface
DNS_INTERFACE_NAME="resin-dns"
DNS_INTERFACE_IP="10.114.102.1"
if [ ! -d "/sys/class/net/${DNS_INTERFACE_NAME}" ]; then
	ip link add name ${DNS_INTERFACE_NAME} type bridge
fi
if [ "$(cat /sys/class/net/${DNS_INTERFACE_NAME}/operstate)" = "down" ]; then
	ip link set ${DNS_INTERFACE_NAME} up
fi
if [ "$(ip addr show ${DNS_INTERFACE_NAME} | grep -o "inet [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" | grep -o "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*")" != "${DNS_INTERFACE_IP}" ]; then
	ip address add ${DNS_INTERFACE_IP}/24 dev ${DNS_INTERFACE_NAME}
fi

exit 0
